//
//  CQNicknameViewController.m
//  HiChat
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQNicknameViewController.h"

@interface CQNicknameViewController ()

@end

@implementation CQNicknameViewController
- (void)dealloc
{
    [_nicknameTextV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    [self create];
}
- (void)create {
    self.nicknameTextV = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 258.0)];
    self.nicknameTextV.textColor = [UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0];
    self.nicknameTextV.font = [UIFont systemFontOfSize:30.0];
    [self.view addSubview:self.nicknameTextV];
    [_nicknameTextV release];

}

- (void)rightAction {
    [self.navigationController popViewControllerAnimated:YES];
    [self.delegate returnNickname:self.nicknameTextV.text];
    
    [self saveNickname];

}
//归档昵称
- (void)saveNickname {
    
    //取出用户名
    NSUserDefaults *useD = [NSUserDefaults standardUserDefaults];
    NSString *getName = [useD objectForKey:@"userName"];
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *str = [NSString stringWithFormat:@"nickname%@", getName];
    NSString *filepath = [path stringByAppendingPathComponent:str];
    [NSKeyedArchiver archiveRootObject:self.nicknameTextV.text toFile:filepath];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationItem.title = @"修改昵称";
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [self.nicknameTextV becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.nicknameTextV resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
